const jwt = require('jsonwebtoken');
const { PRIVATE_KEY, PUBLIC_KEY } = require('../config/secret');



class loginCtroller {
    success(ctx, next) {
        // 1. 获取用户信息
        const { id, name } = ctx.user
        // console.log("ctx.user:id,name", ctx.user);
        // console.log("颁发令牌 start:");
        // 2. 颁发令牌
        // const token = 'xxxsxxx'

        const token = jwt.sign({ id, name }, PRIVATE_KEY, {
            expiresIn: 24 * 60 * 60,
            algorithm: 'RS256'
        })

        // console.log("token get:", token);
        // 3. 返回客户端的信息
        ctx.body = { code: 0, data: { id, name, token } }
    }

    test(ctx, next) {
        ctx.body = 'test通过～'
    }

}

module.exports = new loginCtroller()